package cn.qayy.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;

import cn.qayy.constants.CollectionConstant;
import cn.qayy.constants.Constant;
import cn.qayy.service.Uploadable;
import cn.qayy.utils.ServletContextUtils;
import cn.qayy.utils.StringUtil;

/***
 *@author wy
 *注册库
 */
@SuppressWarnings("all")
public class RegisterDTO implements Serializable,Uploadable{
	/**
	 * 省份
	 */
    private String shengfen="";
    /**
	 * 地市
	 */
    private String city="";
    /**
	 * 县区
	 */
    private String country="";
    /**
	 * 入货乙方
	 */
    private String cname="";
    /**
	 * 备注
	 */
    private String memo="";
    /**
	 * QS名
	 */
    private String zhengheqs="";
    /**
	 * 分部
	 */
    private String addr="";
    /**
	 * 渠道类型
	 */
    private String terminaltype="";
    /**
	 * 等级
	 */
    private String terminallve="";
    /**
     * 修订标识
     */
    private String isUpdate="E";
    /**
	 * id
	 */
    private String id="";

    public RegisterDTO() {
		super();
	}

    /***
     * cname唯一标识该this对象
     */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((cname == null) ? 0 : cname.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		RegisterDTO other = (RegisterDTO) obj;
		if (cname == null) {
			if (other.cname != null)
				return false;
		} else if (!cname.equals(other.cname))
			return false;
		return true;
	}

	public RegisterDTO(String shengfen, String city, String country, String cname, String memo, String zhengheqs,
			String addr, String terminaltype, String terminallve, String isUpdate, String id) {
		this.shengfen = shengfen;
		this.city = city;
		this.country = country;
		this.cname = cname;
		this.memo = memo;
		this.zhengheqs = zhengheqs;
		this.addr = addr;
		this.terminaltype = terminaltype;
		this.terminallve = terminallve;
		this.isUpdate = isUpdate;
		this.id = id;
	}

	public String getTerminaltype() {
		return terminaltype;
	}

	public void setTerminaltype(String terminaltype) {
		this.terminaltype = terminaltype;
	}

	public String getIsUpdate() {
		return isUpdate;
	}

	public void setIsUpdate(String isUpdate) {
		this.isUpdate = isUpdate;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getShengfen() {
        return this.shengfen;
    }

    public void setShengfen(String shengfen) {
        this.shengfen = shengfen;
    }

    public String getCity() {
        return this.city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return this.country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCname() {
        return this.cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public String getMemo() {
        return this.memo;
    }

    public void setMemo(String memo) {
        this.memo = memo;
    }

    public String getAddr() {
        return this.addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    public String getTerminallve() {
        return this.terminallve;
    }

    public void setTerminallve(String terminallve) {
        this.terminallve = terminallve;
    }

	public String getZhengheqs() {
		return zhengheqs;
	}

	public void setZhengheqs(String zhengheqs) {
		this.zhengheqs = zhengheqs;
	}

	@Override
	public String toString() {
		return "RegisterDTO [shengfen=" + shengfen + ", city=" + city + ", country=" + country + ", cname=" + cname
				+ ", memo=" + memo + ", zhengheqs=" + zhengheqs + ", addr=" + addr + ", terminaltype=" + terminaltype
				+ ", terminallve=" + terminallve + ", isUpdate=" + isUpdate + ", id=" + id + "]";
	}

	@Override
	public Uploadable filter(Map<String, List<Uploadable>> map, Set<Uploadable> set, int num) {
		/**容纳错误消息的list*/
		List<Uploadable> mlist = new ArrayList<Uploadable>(Constant.SMALL_CAPACITY);
		/**容纳正确的，最终用于保存到数据库的数据**/
		List<Uploadable> list = new ArrayList<Uploadable>(Constant.SMALL_CAPACITY);
	    
		/***
		 * 过滤掉所有的空格
		 */
		String shengfen = StringUtil.trimAll(this.getShengfen());
	    String city = StringUtil.trimAll(this.getCity());
	    String country = StringUtil.trimAll(this.getCountry());
	    String cname =StringUtil.trimAll(this.getCname());
	    String memo =StringUtil.trimAll(this.getMemo());
	    String qsname =StringUtil.trimAll(this.getZhengheqs());
	    String addr =StringUtil.trimAll(this.getAddr());
	    String department =StringUtil.trimAll(this.getTerminaltype());
	    String terminallve =StringUtil.trimAll(this.getTerminallve());
	    String isUpdate =StringUtil.trimAll(this.getIsUpdate());
	    String id =StringUtil.trimAll(this.getId());
	    
	    /**
	     * 判断是否导入的模板是正确的模板
	     */
	    if(num == 1) {
	    	if(!CollectionConstant.register_mb.get(0).contains(shengfen)
	    			||!CollectionConstant.register_mb.get(1).contains(city)
	    			||!CollectionConstant.register_mb.get(2).contains(country)
	    			||!CollectionConstant.register_mb.get(3).contains(cname)
	    			||!CollectionConstant.register_mb.get(4).contains(memo)
	    			||!CollectionConstant.register_mb.get(5).contains(qsname)
	    			||!CollectionConstant.register_mb.get(6).contains(addr)
	    			||!CollectionConstant.register_mb.get(7).contains(department)
	    			||!CollectionConstant.register_mb.get(8).contains(terminallve)
	    			||!CollectionConstant.register_mb.get(9).contains(isUpdate)
	    			||!CollectionConstant.register_mb.get(10).contains(id)) {
	    		MsgInfo msg = new MsgInfo(Constant.ERROR_MB,Constant.ERROR_MB,Constant.ERROR_MB,Constant.ERROR_NUM+num);
				mlist.add(msg);
				map.put("stop", mlist);
		    	return null;
		    }
	    } else {
	    	/***
	    	 * 获取项目上下文的共享数据
	    	 */
	    	ServletContext servletContext = ServletContextUtils.getServletContext();
	    	/**省份**/
	    	Set<String> provinces = (HashSet<String>)servletContext.getAttribute("provinces");
	    	/**地市**/
	    	Set<String> citys = (HashSet<String>)servletContext.getAttribute("citys");
	    	/**区县**/
	    	Set<String> countrys = (HashSet<String>)servletContext.getAttribute("countrys");
	    	/**终端，入货乙方、出货甲方**/
	    	Set<String> cNames = (HashSet<String>)servletContext.getAttribute("cNames");

	    	/***
		     * 数据合格性校验
		     */
			if(!CollectionConstant.cuFlags.contains(isUpdate)) {
				MsgInfo msg = new MsgInfo(isUpdate,"修订标识",CollectionConstant.cuFlags.toString(),Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			if("E".equals(isUpdate)) {
				if(StringUtil.isBlank(id)) {
					MsgInfo msg = new MsgInfo(isUpdate,"修订标识有误或id有误","修订标识为E或空白时，id不能为空。id为空白时，修订标识只能是N",Constant.ERROR_NUM+num);
					mlist.add(msg);
				}
			}
			/***
			 * 省份、地市、区县是在基础库里出现过
			 */
			if(!provinces.contains(shengfen)) {
				MsgInfo msg = new MsgInfo(shengfen,"省份",Constant.BASIC_DATA,Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			if(!citys.contains(city)) {
				MsgInfo msg = new MsgInfo(city,"地市",Constant.BASIC_DATA,Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			if(!countrys.contains(country)) {
				MsgInfo msg = new MsgInfo(country,"区县",Constant.BASIC_DATA,Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			
			/***
			 * 判断是否在数据库中存在
			 * 注册库中入货乙方是唯一约束的，不允许修改入货乙方
			 */
			if(isUpdate.equals("N")) {
				if(cNames.contains(cname)) {
					MsgInfo msg = new MsgInfo(cname,Constant.REPEATABLE,"已在【注册库】中存在了，请检查",Constant.ERROR_NUM+num);
					mlist.add(msg);
				}
			} 
			
			/**
			 * 判断入货乙方是否为空
			 */
			if(StringUtil.isBlank(cname)) {
				MsgInfo msg = new MsgInfo(cname,"入货乙方",Constant.NOT_BLANK_MSG,Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			/**
			 * 判断Qs名是否为空
			 */
			if(StringUtil.isBlank(qsname)) {
				MsgInfo msg = new MsgInfo(qsname,"QS名",Constant.NOT_BLANK_MSG,Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			
			/**
			 * 判断等级是否为空
			 */
			if(StringUtil.isBlank(terminallve)) {
				MsgInfo msg = new MsgInfo(terminallve,"等级",Constant.NOT_BLANK_MSG,Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			
			/***
			 * 判断渠道
			 */
			if(!CollectionConstant.qudaos.contains(department)) {
				MsgInfo msg = new MsgInfo(department,"渠道类型",CollectionConstant.qudaos.toString(),Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			
			/***
			 * 返回处理后的结果数据
			 */
			if(!mlist.isEmpty()) {
				map.put("fail"+num, mlist);
			} 
			/***把验证通过的合格数据返回**/
			this.setAll(shengfen,city,country,cname,memo,qsname,addr,department,terminallve,isUpdate,id);
			//不是空行才添加
			if(StringUtil.isNotBlank(shengfen)&&StringUtil.isNotBlank(city)&&StringUtil.isNotBlank(country)) {
				set.add(this);
			}
			return this;
	    }
		return null;
	}
	
	public void setAll(String shengfen, String city, String country, String cname, String memo, String qsname,
			String addr, String department, String terminallve, String isUpdate, String id) {
		this.shengfen = shengfen;
		this.city = city;
		this.country = country;
		this.cname = cname;
		this.memo = memo;
		this.zhengheqs = qsname;
		this.addr = addr;
		this.terminaltype = department;
		this.terminallve = terminallve;
		this.isUpdate = isUpdate;
		this.id = id;
	}

	@Override
	public String flag() {
		return this.isUpdate;
	}
}
